package com.bugjpa.controller;

import com.bugjpa.domain.Bug;
import com.bugjpa.domain.BugLevel;
import com.bugjpa.service.IBugService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mayuanbao
 * @date 2019-01-13 22:01
 */
@Controller
public class BugController {
    @Resource(name = "bugService")
    private IBugService bugService;

    /**
     * 返回表数据
     *
     * @return java.util.List<com.bugjpa.domain.Bug>
     * @author mayuanbao
     * @date 2019-01-23 21:12
     */
    @ResponseBody
    @RequestMapping(value = "/getbug")
    public Map<String, Object> findAll(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page")) - 1;
        int limit = Integer.parseInt(request.getParameter("limit"));
        List<Bug> bugList = bugService.findAll(page, limit).getContent();
        Long count = bugService.count();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", count);
        map.put("msg", "");
        map.put("data", bugList);
        return map;
    }

    /**
     * bug列表页
     * @return java.lang.String
     * @author mayuanbao
     * @date 2019-01-23 21:14
     */
    @RequestMapping(value = "/bugslist", method = RequestMethod.GET)
    public String bugsList() {
        return "echarts/bugslist";
    }

    /**
     * pie页面
     * @return java.lang.String
     * @author mayuanbao
     * @date 2019-01-24 21:03
     */
    @RequestMapping(value = "/pie", method = RequestMethod.GET)
    public String pie() {
        return "pie/pie";
    }

    @ResponseBody
    @RequestMapping(value = "/getpie")
    public Map<String, Object> findGroupByLevel() {
        Long count = bugService.count();
        Map<String, Object> objectMap = new HashMap<>();
        List<Object[]> bugList = bugService.findGroupByLevel();
        List<Object> pieList = new ArrayList<>();
        List<Object> tableList = new ArrayList<>();
        for (Object[] obj : bugList) {
            Map<String, Object> pieMap = new HashMap<>();
            pieMap.put("name", obj[0]);
            pieMap.put("value", obj[1]);
            Map<String, Object> tableMap = new HashMap<>();
            tableMap.put("level", obj[0]);
            tableMap.put("count", obj[1]);
            DecimalFormat df = new DecimalFormat("0.00");
            float proportion = (float) Long.parseLong(obj[1].toString()) / count * 100;
            tableMap.put("proportion", df.format(proportion));
            tableList.add(tableMap);
            pieList.add(pieMap);
        }
        objectMap.put("pie", pieList);
        objectMap.put("buglist", tableList);
        return objectMap;
    }

    @ResponseBody
    @RequestMapping("/getmap")
    public Map<String, Object> getMap() {
        List<Map<String, String>> groupByLevels = bugService.findGroupByLevels();
        System.out.println(groupByLevels);
        return null;
    }

}
